package xiaozhi.modules.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import lombok.AllArgsConstructor;
import xiaozhi.common.constant.Constant;
import xiaozhi.common.page.PageData;
import xiaozhi.common.service.impl.BaseServiceImpl;
import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.common.utils.JsonUtils;
import xiaozhi.modules.sys.dao.SysParamsDao;
import xiaozhi.modules.sys.dto.SysParamsDTO;
import xiaozhi.modules.sys.entity.SysParamsEntity;
import xiaozhi.modules.sys.service.SysParamsService;

/**
 * 参数管理
 */
@Service
@AllArgsConstructor
public class SysParamsServiceImpl extends BaseServiceImpl<SysParamsDao, SysParamsEntity> implements SysParamsService {
    private final SysParamsDao sysParamsDao;

    @Override
    public PageData<SysParamsDTO> page(Map<String, Object> params) {
        IPage<SysParamsEntity> page = baseDao.selectPage(
            getPage(params, Constant.CREATE_DATE, false),
            getWrapper(params)
        );

        return getPageData(page, SysParamsDTO.class);
    }

    @Override
    public List<SysParamsDTO> list(Map<String, Object> params) {
        List<SysParamsEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, SysParamsDTO.class);
    }

    private QueryWrapper<SysParamsEntity> getWrapper(Map<String, Object> params) {
        String paramCode = (String) params.get("paramCode");

        QueryWrapper<SysParamsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(paramCode), "param_code", paramCode);

        return wrapper;
    }

    @Override
    public SysParamsDTO get(Long id) {
        SysParamsEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, SysParamsDTO.class);
    }

    @Override
    public void save(SysParamsDTO dto) {
        SysParamsEntity entity = ConvertUtils.sourceToTarget(dto, SysParamsEntity.class);
        insert(entity);
    }

    @Override
    public void update(SysParamsDTO dto) {
        SysParamsEntity entity = ConvertUtils.sourceToTarget(dto, SysParamsEntity.class);
        updateById(entity);
    }

    @Override
    public void delete(String[] ids) {
        baseDao.deleteBatchIds(List.of(ids));
    }

    @Override
    public String getValue(String paramCode, Boolean fromCache) {
        // 实际项目中可能会有缓存逻辑，这里简化处理
        return sysParamsDao.getValueByCode(paramCode);
    }

    @Override
    public <T> T getValueObject(String paramCode, Class<T> clazz) {
        String value = getValue(paramCode, true);
        if(StringUtils.isBlank(value)){
            return null;
        }

        return JsonUtils.parseObject(value, clazz);
    }

    @Override
    public int updateValueByCode(String paramCode, String paramValue) {
        return sysParamsDao.updateValueByCode(paramCode, paramValue);
    }

    @Override
    public void initServerSecret() {
        // 实现服务器密钥初始化逻辑
        // 如果不存在则创建，存在则不处理
        String secretKey = getValue(Constant.SERVER_SECRET, true);
        if (StringUtils.isBlank(secretKey)) {
            // 生成随机密钥并保存
            String newSecret = generateRandomSecret();
            updateValueByCode(Constant.SERVER_SECRET, newSecret);
        }
    }
    
    private String generateRandomSecret() {
        // 生成随机密钥的逻辑，可以根据实际需求实现
        return java.util.UUID.randomUUID().toString().replace("-", "");
    }
}
